package com.engineering.elink.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.common.TypeEnum;
import com.engineering.elink.dao.BlogMapper;
import com.engineering.elink.dao.CollectionMapper;
import com.engineering.elink.dao.CompanyMapper;
import com.engineering.elink.dao.CourseMapper;
import com.engineering.elink.dao.FileMapper;
import com.engineering.elink.dao.JobMapper;
import com.engineering.elink.dao.NewsMapper;
import com.engineering.elink.dao.PostbarMapper;
import com.engineering.elink.dao.SkillMapper;
import com.engineering.elink.dao.ThesisMapper;
import com.engineering.elink.dao.VideoMapper;
import com.engineering.elink.domain.Blog;
import com.engineering.elink.domain.Collection;
import com.engineering.elink.domain.Company;
import com.engineering.elink.domain.Course;
import com.engineering.elink.domain.File;
import com.engineering.elink.domain.Job;
import com.engineering.elink.domain.News;
import com.engineering.elink.domain.Postbar;
import com.engineering.elink.domain.Thesis;
import com.engineering.elink.service.CollectionService;
import com.engineering.elink.util.MapConvert;

@Service
public class CollectionServiceImpl implements CollectionService {
	@Autowired
	private CollectionMapper collectionMapper;
	@Autowired
	private BlogMapper blogMappper;
	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private JobMapper jobMapper;
	@Autowired
	private FileMapper fileMapper;
	@Autowired
	private ThesisMapper thesisMapper;
	@Autowired
	private NewsMapper newsMapper;
	@Autowired
	private VideoMapper videoMapper;
	@Autowired
	private CourseMapper courseMapper;
	@Autowired
	private SkillMapper skillMapper;
	@Autowired
	private PostbarMapper  postbarMapper;

	@Override
	public Map<String, Object> updateCollect(Collection collection) {
		int count = collectionMapper.select(collection.getUserId(),collection.getFkId());
		if (count == 0) { // 表示没有收藏过
			collection.setCollectionTime(new Date());
			collectionMapper.insertSelective(collection);

			if (collection.getType() ==TypeEnum.BLOG.getIndex()) {// 博文收藏
				// 更新博文的收藏数
				Blog blog = blogMappper.selectByPrimaryKey(collection.getFkId());
				blog.setCollectionCount(blog.getCollectionCount() + 1);
				blogMappper.updateByPrimaryKeySelective(blog);
				return MapConvert.getMap(ResultType.SUCCESS,blog.getCollectionCount());
			} else if (collection.getType() == TypeEnum.COMPANY.getIndex()) {// 企业收藏
				// 更新企业的收藏数
					Company company=	companyMapper.selectByPrimaryKey(collection.getFkId());
					company.setCollectionCount(company.getCollectionCount()+1);
					companyMapper.updateByPrimaryKeySelective(company);
					return MapConvert.getMap(ResultType.SUCCESS,company.getCollectionCount());
			} else if (collection.getType() == TypeEnum.JOB.getIndex()) {// 职位收藏
					// 更新职位的收藏数
					Job job =  jobMapper.selectByPrimaryKey(collection.getFkId());
					job.setCollectionCount(job.getCollectionCount()+1);
					jobMapper.updateByPrimaryKeySelective(job);
					return MapConvert.getMap(ResultType.SUCCESS,job.getCollectionCount());
			}else if (collection.getType() == TypeEnum.FILE.getIndex()) {// 资料收藏
				File file = fileMapper.selectByPrimaryKey(collection.getFkId());
				file.setCollectionCount(file.getCollectionCount()+1);
				fileMapper.updateByPrimaryKeySelective(file);
				return MapConvert.getMap(ResultType.SUCCESS,file.getCollectionCount());
			}else if (collection.getType() == TypeEnum.LIBRARY.getIndex()) {// 论文收藏
				Thesis thesis = thesisMapper.selectByPrimaryKey(collection.getFkId());
				thesis.setCollectionCount(thesis.getCollectionCount()+1);
				thesisMapper.updateByPrimaryKeySelective(thesis);
				return MapConvert.getMap(ResultType.SUCCESS,thesis.getCollectionCount());
			}else if (collection.getType() == TypeEnum.NEWS.getIndex()) {// 资讯收藏
				News news = newsMapper.selectByPrimaryKey(collection.getFkId());
				news.setCollectionCount(news.getCollectionCount()+1);
				newsMapper.updateByPrimaryKeySelective(news);
				return MapConvert.getMap(ResultType.SUCCESS,news.getCollectionCount());
			}else if (collection.getType() == TypeEnum.COURSE.getIndex()) {// 课程收藏
				Course course = courseMapper.selectByPrimaryKey(Integer.parseInt(collection.getFkId()));
				course.setCollectionCount(course.getCollectionCount()+1);
				courseMapper.updateByPrimaryKeySelective(course);
				return MapConvert.getMap(ResultType.SUCCESS,course.getCollectionCount());
			}else if (collection.getType() == TypeEnum.POSTBAR.getIndex()) {// 百问百答收藏
				Postbar postbar = postbarMapper.selectByPrimaryKey(collection.getFkId());
				postbar.setCollectionCount(postbar.getCollectionCount()+1);
				postbarMapper.updateByPrimaryKeySelective(postbar);
				return MapConvert.getMap(ResultType.SUCCESS,postbar.getCollectionCount());
			}
		} else {//取消收藏
			collectionMapper.delete(collection.getUserId(),collection.getFkId());
			
			if (collection.getType() == TypeEnum.BLOG.getIndex()) {// 博文收藏
				// 更新博文的收藏数
				Blog blog = blogMappper.selectByPrimaryKey(collection.getFkId());
				blog.setCollectionCount(blog.getCollectionCount() - 1);
				blogMappper.updateByPrimaryKeySelective(blog);
			} else if (collection.getType() == TypeEnum.COMPANY.getIndex()) {// 企业收藏
				// 更新企业的收藏数
				Company company=	companyMapper.selectByPrimaryKey(collection.getFkId());
				company.setCollectionCount(company.getCollectionCount()-1);
				companyMapper.updateByPrimaryKeySelective(company);
			} else if (collection.getType() == TypeEnum.JOB.getIndex()) {// 职位收藏
				// 更新职位的收藏数
				Job job =  jobMapper.selectByPrimaryKey(collection.getFkId());
				job.setCollectionCount(job.getCollectionCount()-1);
				jobMapper.updateByPrimaryKeySelective(job);
			}else if (collection.getType() == TypeEnum.FILE.getIndex()) {// 资料收藏
				File file = fileMapper.selectByPrimaryKey(collection.getFkId());
				file.setCollectionCount(file.getCollectionCount()-1);
				fileMapper.updateByPrimaryKeySelective(file);
			}else if (collection.getType() == TypeEnum.LIBRARY.getIndex()) {// 论文收藏
				Thesis thesis = thesisMapper.selectByPrimaryKey(collection.getFkId());
				thesis.setCollectionCount(thesis.getCollectionCount()-1);
				thesisMapper.updateByPrimaryKeySelective(thesis);
			}else if (collection.getType() == TypeEnum.NEWS.getIndex()) {// 资讯收藏
				News news = newsMapper.selectByPrimaryKey(collection.getFkId());
				news.setCollectionCount(news.getCollectionCount()-1);
				newsMapper.updateByPrimaryKeySelective(news);
			}else if (collection.getType() == TypeEnum.COURSE.getIndex()) {// 课程收藏
				Course course = courseMapper.selectByPrimaryKey(Integer.parseInt(collection.getFkId()));
				course.setCollectionCount(course.getCollectionCount()-1);
				courseMapper.updateByPrimaryKeySelective(course);
			}else if (collection.getType() == TypeEnum.POSTBAR.getIndex()) {// 百问百答收藏
				Postbar postbar = postbarMapper.selectByPrimaryKey(collection.getFkId());
				postbar.setCollectionCount(postbar.getCollectionCount()-1);
				postbarMapper.updateByPrimaryKeySelective(postbar);
//				return MapConvert.getMap(ResultType.SUCCESS,postbar.getCollectionCount());
			}
			return MapConvert.getMap(ResultType.CANCEL_COLLECTION);
		}
		return MapConvert.getMap(ResultType.SUCCESS);
	}

	@Override
	public Map<String, Object> getMyCollect(BaseCo co, String userId,int type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", collectionMapper.selectMyCollect(co,userId,type));
		map.put("Pagination",   new Pagination(co, collectionMapper.selectCountMyCollect(userId,type)) );
		return MapConvert.getMap(ResultType.SUCCESS,map);
	}
}
